package com.onenine.app.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.onenine.app.R;
import com.onenine.app.model.LabelItem;
import com.onenine.common.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间：2018/5/23 0023 17:54
 * 创建人：fly
 * 功能描述：单行带网格标签
 */
public class SingleSelectLabelView extends SingleSelectView {

    private FlexboxLayout mFlexLayout;
    private List<LabelItem> mLabelList;
    private LayoutInflater mLayoutInflater;

    public SingleSelectLabelView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SingleSelectLabelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_single_select_flex, this);
    }

    @Override
    protected boolean needLayoutInflate() {
        return false;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLayoutInflater = LayoutInflater.from(getContext());
        mFlexLayout = findViewById(R.id.flex_boxy_layout);
    }

    public void showLabelView(List<LabelItem> dataList) {
        this.mLabelList = dataList;
        int labelWidth = getResources().getDimensionPixelOffset(R.dimen.label_width_large);
        int labelHeight = getResources().getDimensionPixelOffset(R.dimen.label_height_large);
        int labelMargin = SizeUtils.dp2px(5);
        mFlexLayout.removeAllViews();
        for (LabelItem label : mLabelList) {
            View labelView = mLayoutInflater.inflate(R.layout.layout_label_item, null, false);
            TextView tvLabel = labelView.findViewById(R.id.tv_label);
            tvLabel.setText(label.getLabelName());
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(labelWidth, labelHeight);
            layoutParams.setMargins(labelMargin, labelMargin, labelMargin, labelMargin);
            mFlexLayout.addView(labelView, -1, layoutParams);
        }
    }

    public List<LabelItem> getLabelList() {
        if (mLabelList == null) {
            return new ArrayList<>();
        }
        return mLabelList;
    }

}
